package com.sgxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sgxy.entity.NoticeInf;
import com.sgxy.entity.UserInf;
import com.sgxy.service.NoticeInfService;
import com.sgxy.service.NoticeInfService;
import net.sf.jsqlparser.statement.select.MinusOp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeInfController {
    private final NoticeInfService noticeInfService;
    @Autowired
    public NoticeInfController(NoticeInfService NoticeInfService) {
        this.noticeInfService = NoticeInfService;
    }
    //集合根据id查询，模糊查询，分页查询
    @RequestMapping("/selectnotice")
    public ModelAndView selectNoticeAction(NoticeInf noticeInf,
                                             @RequestParam(value = "page", defaultValue = "1")Integer page){
        System.out.println(noticeInf);
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page,10);
        List<NoticeInf> NoticeInfs = noticeInfService.QueryAllFNoticeInfs(noticeInf);
        PageInfo<NoticeInf> noticeInfPageInfo = new PageInfo<>(NoticeInfs);
        modelAndView.addObject("noticeInfPageInfo",noticeInfPageInfo);
        modelAndView.addObject("noticeInf",noticeInf);
        modelAndView.setViewName("/notice/notice.jsp");
        return modelAndView;
    }
    //添加
    @RequestMapping("/toAddnotice")
    public ModelAndView toAddnoticeAction(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/notice/showAddNotice.jsp");
        return modelAndView;
    }

    @RequestMapping("/addnotice")
    public ModelAndView addNoticeAction(NoticeInf noticeInf){
        ModelAndView modelAndView  = new ModelAndView();
        int i = noticeInfService.AddNoticeInf(noticeInf);
//        if(i>0){
//            modelAndView.setViewName("notice.jsp");
//        }
        modelAndView.setViewName("redirect:/notice/toAddnotice");
        return modelAndView;
    }
    //更新

    @RequestMapping("/toUpdatenotice")
    public ModelAndView toUpdatenoticeAction(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        NoticeInf noticeInf = noticeInfService.QueryNoticeInf(id);
        modelAndView.addObject("noticeInf",noticeInf);
        modelAndView.setViewName("/notice/showUpdateNotice.jsp");
        return modelAndView;
    }

    @RequestMapping("/updatenotice")
    public ModelAndView updateNoticeAction(NoticeInf noticeInf){
        System.out.println(noticeInf);
        ModelAndView modelAndView = new ModelAndView();
        int i = noticeInfService.UpdateNoticeInf(noticeInf);
//        if(i>0){
//            modelAndView.setViewName("notice.jsp");
//        }
        modelAndView.setViewName("redirect:/notice/selectnotice");
        return modelAndView;
    }
    //单个个删除和多个删除，数据珍贵，目前用sql语句select代替中
    // ↑
    // 已更换为delete 2022年1月10日10点24分
    @RequestMapping("/deletenotice")
    public ModelAndView deleteNotice(String ids){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(ids);
        String[] split = ids.split(",");
        List<String> list = new ArrayList<String>();
        for(int i=0;i<split.length;i++){
            list.add(split[i]);
        }
        System.out.println(ids);
        int i = noticeInfService.DeleteNoticeInf(list);
//        int i=1;
//        if(i>0){
//            modelAndView.setViewName("notice.jsp");
//        }
        modelAndView.setViewName("redirect:/notice/selectnotice");
        return modelAndView;
    }

    @RequestMapping("toPreviewNotice")
    public ModelAndView toPreviewNoticeAction(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        NoticeInf noticeInf = noticeInfService.QueryNoticeInf(id);
        modelAndView.addObject("noticeInf",noticeInf);
        modelAndView.setViewName("/notice/previewNotice.jsp");
        return modelAndView;
    }
}
